package com.bjpowernode.boss.mvc.service.impl;

import java.math.BigDecimal;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjpowernode.boss.convert.UserInfoConvert;
import com.bjpowernode.boss.domain.UserAccount;
import com.bjpowernode.boss.domain.UserInfo;
import com.bjpowernode.boss.enums.ajax.Code;
import com.bjpowernode.boss.enums.buz.PublicStatus;
import com.bjpowernode.boss.exception.BossException;
import com.bjpowernode.boss.mapper.UserAccountMapper;
import com.bjpowernode.boss.mapper.UserInfoMapper;
import com.bjpowernode.boss.mvc.model.query.UserInfoQuery;
import com.bjpowernode.boss.mvc.model.vo.MatchUserInfoVo;
import com.bjpowernode.boss.mvc.service.UserInfoService;
import com.bjpowernode.boss.utils.NoUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 客户信息 业务层实现类
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Resource
    private UserInfoConvert userInfoConvert;

    @Override
    public void saveOrEditUserByUser(UserInfoQuery userInfoQuery) {
        // 判断userInfoQuery 的id 是否存在 , 确定是 添加操作 还是 更新操作
        if (StrUtil.isBlank(userInfoQuery.getId())) {
            // 是空 , 进行添加操作
            saveUser(userInfoQuery);
        } else {
            // 不是空
            editUserByUserId(userInfoQuery);
        }
    }

    @Override
    public IPage<UserInfo> selectUserInfoPageList(Integer pageNo, int pageSize) {
        return userInfoMapper.selectPage(new Page<>(pageNo, pageSize), new QueryWrapper<UserInfo>().orderByAsc("create_time"));
    }

    @Override
    public UserInfo loadUserInfoByUserNo(String userNo) {
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserNo, userNo));
    }

    @Override
    public void editUserInfoStatusByUserNo(String userNo) {
        // 查询该用户 根据userNo
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserNo, userNo));
        // 判空
        if (userInfo == null) {
            throw new BossException(Code.USER_INVALID_NOT_USE);
        }
        // 获取用户的状态
        String status = PublicStatus.ENABLE.name(); // ENABLE
        if (status.equalsIgnoreCase(userInfo.getStatus())) {
            status = PublicStatus.DISABLE.name();
        }
        UserInfo update = new UserInfo();
        update.setId(userInfo.getId());
        update.setStatus(status);
        userInfoMapper.updateById(update);
    }

    @Override
    public List<UserInfo> matchFindUserInfoList(String shopName) {
        return userInfoMapper.selectList(
                new LambdaQueryWrapper<UserInfo>()
                        .select(UserInfo::getUserNo, UserInfo::getShopName)
                        .like(UserInfo::getShopName, shopName));
    }

    /**
     * 添加用户操作
     *
     * @param userInfoQuery
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveUser(UserInfoQuery userInfoQuery) {
        // 判断要添加的数据是否存在在数据库中
        UserInfo selectExist = userInfoMapper
                .selectOne(new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getLoginName, userInfoQuery.getLoginName()));
        // 判断是否存在 :===>  存在 ,抛出异常
        if (selectExist != null) {
            throw new BossException(Code.USER_LOGIN_NAME_EXISTS);
        }
        // 不存在 , 进行添加操作
        UserInfo userInfo = new UserInfo();
        // 拷贝
        BeanUtils.copyProperties(userInfoQuery, userInfo);
        // 赋值其他
        userInfo.setId(NoUtil.uuid());
        userInfo.setUserNo(NoUtil.getUserNo());
        userInfo.setAccountNo(NoUtil.getAccountNo());
        userInfo.setPassword(NoUtil.uuid());
        userInfo.setPayPwd(NoUtil.uuid());
        // ENABLE.name() :  Returns the name of this enum constant
        userInfo.setStatus(PublicStatus.ENABLE.name());
        userInfo.setCreateTime(new Date());
        userInfo.setRemark("创建商户");

        // 添加商户
        int insert = userInfoMapper.insert(userInfo);

        // 创建商户资金账号
        UserAccount userAccount = new UserAccount();
        userAccount.setId(NoUtil.uuid());
        userAccount.setUserNo(userInfo.getUserNo());
        userAccount.setAccountNo(userInfo.getAccountNo());
        userAccount.setBalance(new BigDecimal(0));
        userAccount.setTotalIncome(new BigDecimal(0));
        userAccount.setTotalExpend(new BigDecimal(0));
        userAccount.setStatus(PublicStatus.ENABLE.name());
        userAccount.setRemark("创建资金账户");
        userAccount.setEditTime(new Date());
        int i = userAccountMapper.insert(userAccount);
    }


    /**
     * 编辑操作
     *
     * @param userInfoQuery
     */
    public void editUserByUserId(UserInfoQuery userInfoQuery) {
        userInfoMapper.updateById(userInfoConvert.toConvertUserInfo(userInfoQuery));
    }
}
